﻿using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Data;

namespace SSCCMembership.Web.Services.Members
{
    [ErrorHandlerBehavior]
    [ServiceContract(Namespace = "")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class MedicalEmergencyService
    {
        [OperationContract]
        public MedicalAndEmergencyDetails LoadMedicalAndEMData(int MemberID)
        {
            SecurityTester.UserCanEditMember(MemberID);

            //var Me = DC.SSCC_DAT_Member.Where((M) => M.ID == MemberID).First();

            using (var Q = SSCCDatabase.Query("SELECT FirstName, Surname, EMContactName, EMContactPhone, EMPhoneOptOut, EMContactMobile, EMMobileOptOut, CanSwim, DrName, DrPhone, Medical FROM SSCC_DAT_Member WHERE (ID = @ID)",
                new SqlParameter("@ID", SqlDbType.Int), MemberID))
            {
                var Me = Q.First();
                return new MedicalAndEmergencyDetails()
                {
                    ID = MemberID,
                    FullName = String.Format("{0} {1}", Me["FirstName"], Me["Surname"]),
                    EMContactName = Me["EMContactName"].ToString(),
                    EMContactPhone = Me["EMContactPhone"].ToString(),
                    EMPhoneOptOut = Me["EMPhoneOptOut"].Nullable<bool?>(),
                    EMContactMobile = Me["EMContactMobile"].ToString(),
                    EMMobileOptOut = Me["EMMobileOptOut"].Nullable<bool?>(),
                    CanSwim = Me["CanSwim"].Nullable<bool?>(),
                    DrName = Me["DrName"].ToString(),
                    DrPhone = Me["DrPhone"].ToString(),
                    Medical = Me["Medical"].ToString()
                };
            }

            //return new MedicalAndEmergencyDetails() 
            //{ 
            //    ID= MemberID,
            //    FullName = Me.FirstName + " " + Me.Surname,
            //    EMContactName = Me.EMContactName,
            //    EMContactPhone = Me.EMContactPhone,
            //    EMPhoneOptOut = Me.EMPhoneOptOut,
            //    EMContactMobile = Me.EMContactMobile,
            //    EMMobileOptOut = Me.EMMobileOptOut,
            //    CanSwim=Me.CanSwim,
            //    DrName = Me.DrName,
            //    DrPhone = Me.DrPhone,
            //    Medical = Me.Medical
            //};
        }
        [OperationContract]
        public void UpdatePropertyValue(int MemberID, string PropertyName, object value)
        {
            SecurityTester.UserCanEditMember(MemberID);
            SqlParameter P = null;
            switch (PropertyName)
            {
                case "EMContactName":
                case "EMContactPhone":
                case "EMContactMobile":
                case "DrName":
                case "DrPhone":
                case "Medical":
                    P = new SqlParameter("@value", SqlDbType.VarChar);
                    break;
                case "EMPhoneOptOut":
                case "EMMobileOptOut":
                case "CanSwim":
                    P = new SqlParameter("@value", SqlDbType.Bit);
                    break;
                default:
                    return;
            }
            SSCCDatabase.ExecuteStatement("UPDATE SSCC_DAT_Member SET " + PropertyName + "=@Value WHERE ID=@ID"
                , new Dictionary<SqlParameter, object>()
                {
                    {P, value.ToDBNullable()},
                    {new SqlParameter("@ID", SqlDbType.Int), MemberID}
                });
        }
    }
    public class MedicalAndEmergencyDetails
    {
        //Read Only
        public int ID { get; set; }
        public string FullName { get; set; }

        //Read and Write
        public string EMContactName { get; set; }
        public string EMContactPhone { get; set; }
        public bool? EMPhoneOptOut { get; set; }
        public string EMContactMobile { get; set; }
        public bool? EMMobileOptOut { get; set; }
        public bool? CanSwim { get; set; }
        public string DrName { get; set; }
        public string DrPhone { get; set; }
        public string Medical { get; set; }
    }
}
